package com.zzvcom.webapp.action.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Inquiry;
import com.zzvcom.service.admin.InquiryManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.DateUtil;
import com.zzvcom.utils.ExtMessage;

@Controller
@Scope("prototype")
public class InquiryAction
{

    private Inquiry inquiry;

    private InquiryManager manager;

    private ExtMessage msg = new ExtMessage();// 返回信息

    private Map rs = new HashMap();// 结果集合

    private ResultPage resultPage;

    private Integer start;

    private Integer limit;

    public String execute()
    {
        if (resultPage == null)
        {
            resultPage = new ResultPage();
        }
        resultPage.startLimitSetting(start, limit);
        DetachedCriteria dc = DetachedCriteria.forClass(Inquiry.class);
        if(inquiry !=null && StringUtils.isNotBlank(inquiry.getName()))
        {
            dc.add(Restrictions.like("name", inquiry.getName(), MatchMode.START));
        }
        dc.addOrder(Order.asc("isnew"));
        dc.addOrder(Order.desc("addtime"));
        resultPage.setDetachedCriteria(dc);
        manager.getResultPageByDetachedCriteria(resultPage);
        rs.put("root", resultPage.getResult());
        rs.put("records", resultPage.getTotalRecord());
        return "rs";
    }

    public String save()
    {
        if (StringUtils.isBlank(inquiry.getId()))
        {
            inquiry.setId(null);
            inquiry.setAddtime(DateUtil.dateToNum14(new Date()));
        } 
        else
        {
        	inquiry.setIsnew("1");
        }
        manager.save(inquiry);
        msg.setInfo("保存成功");
        return "msg";
    }

    public String delete()
    {
        inquiry = manager.get(inquiry.getId());
        manager.delete(inquiry);
        msg.setInfo("删除成功");
        return "msg";
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public Inquiry getInquiry()
    {
        return inquiry;
    }

    public void setInquiry(Inquiry inquiry)
    {
        this.inquiry = inquiry;
    }

    public InquiryManager getManager()
    {
        return manager;
    }

    @Autowired
    public void setManager(InquiryManager manager)
    {
        this.manager = manager;
    }

    public ExtMessage getMsg()
    {
        return msg;
    }

    public void setMsg(ExtMessage msg)
    {
        this.msg = msg;
    }

    public Map getRs()
    {
        return rs;
    }

    public void setRs(Map rs)
    {
        this.rs = rs;
    }

    public Integer getStart()
    {
        return start;
    }

    public void setStart(Integer start)
    {
        this.start = start;
    }

    public Integer getLimit()
    {
        return limit;
    }

    public void setLimit(Integer limit)
    {
        this.limit = limit;
    }

}
